package com.example.gulimallproduct.controller;

import java.util.Arrays;
import java.util.Map;

import com.example.gulimallproduct.request.SaveAttrRequest;
import com.example.gulimallproduct.request.UpdateAttrRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.example.gulimallproduct.entity.AttrEntity;
import com.example.gulimallproduct.service.AttrService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 商品属性
 *
 * @author zzl
 * @email zzl@gmail.com
 * @date 2025-10-24 15:22:38
 */
@RestController
@RequestMapping("/attr")
@Slf4j
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     * 基本属性列表
     */
    @GetMapping("/base/list/{catId}")
    public R listBase(@PathVariable("catId") Long catId,@RequestParam Map<String, Object> params){
        log.info("获取基本属性列表:{}", params);
        return attrService.selectPageWhthBase(params, catId, 1);
    }
    /**
     * 销售属性列表
     */
    @GetMapping("/sale/list/{catId}")
    public R listSale(@PathVariable("catId") Long catId,@RequestParam Map<String, Object> params){
        log.info("获取销售属性列表:{}", params);
        return attrService.selectPageWhthBase(params, catId,0);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        log.info("获取属性信息:{}", attrId);
        return attrService.selectInfoById(attrId);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@Validated @RequestBody SaveAttrRequest attr){
        log.info("新增属性:{}", attr);
        return attrService.saveAttr(attr);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UpdateAttrRequest attr){
        log.info("修改属性:{}", attr);
        return attrService.updateAttr(attr);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
